package io.github.talelin.latticy.service.impl;

import io.github.talelin.latticy.common.configuration.RabbitConfig;
import io.github.talelin.latticy.common.pojo.Mail;
import io.github.talelin.latticy.common.util.SnowFlake;
import io.github.talelin.latticy.model.MsgLogDO;
import io.github.talelin.latticy.mq.MessageHelper;
import io.github.talelin.latticy.service.MsgLogService;
import io.github.talelin.latticy.service.TestService;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class TestServiceImpl implements TestService {

    @Autowired
    private MsgLogService msgLogService;

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Override
    public void testIdempotence() {
    }

    @Override
    public void accessLimit() {
    }

    @Override
    public void send(Mail mail) {
        //生成订单号
        SnowFlake idWorker = new SnowFlake(0, 0);
        Long msgId = idWorker.nextId();
        mail.setMsgId(msgId);

        MsgLogDO msgLog = new MsgLogDO(msgId, mail, RabbitConfig.MAIL_EXCHANGE_NAME, RabbitConfig.MAIL_ROUTING_KEY_NAME);
        msgLogService.save(msgLog);// 消息入库

        CorrelationData correlationData = new CorrelationData(String.valueOf(msgId));
        rabbitTemplate.convertAndSend(RabbitConfig.MAIL_EXCHANGE_NAME, RabbitConfig.MAIL_ROUTING_KEY_NAME, MessageHelper.objToMsg(mail), correlationData);// 发送消息
    }

}
